package com.example.adouble.cehuamenu.ui.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.adouble.cehuamenu.R;
import com.example.adouble.cehuamenu.mode.User;
import com.example.adouble.cehuamenu.util.ToastUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobQueryResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SQLQueryListener;

/**
 * 登陆
 */
public class LoginActivity extends BaseActivity {

    @BindView(R.id.btnLogin)
    Button btnLogin;

    @BindView(R.id.edtUsername)
    EditText edtUsername;

    @BindView(R.id.edtPassword)
    EditText edtPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        intiView();
    }

    public void intiView() {
        btnLogin.getBackground().setAlpha(90);
    }

    /**
     * 登陆
     *
     * @param v
     */
    @OnClick(R.id.btnLogin)
    public void login(View v) {
        final String name = edtUsername.getText().toString();
        final String pwd = edtPassword.getText().toString();
        if (name.isEmpty() || pwd.isEmpty()) {
            ToastUtils.show("账号或密码不能为空");
            return;
        }
        String bql = "select * from User where name = ? and pwd = ?";
        BmobQuery<User> query = new BmobQuery<User>();
        query.setSQL(bql);
        query.setPreparedParams(new Object[]{name, pwd});
        query.doSQLQuery(new SQLQueryListener<User>() {
            @Override
            public void done(BmobQueryResult<User> result, BmobException e) {
                if (e == null) {
                    List<User> list = result.getResults();
                    String uname = list.get(0).getName();
                    String id = list.get(0).getObjectId();
                    if (list != null) {
                        saveInfo(uname, id);
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                        ToastUtils.show("登陆成功");
                    }
                } else {
                    ToastUtils.show("账号或密码错误");
                }
            }
        });
    }

    /**
     * 保存登录信息
     * @param uname
     * @param id
     */
    public void saveInfo(String uname, String id) {
        SharedPreferences sp = getSharedPreferences("login", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("uname", uname);
        editor.putString("id", id);
        editor.commit();
    }


    public void register(View v) {
        Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
        startActivity(intent);
    }

    public void update(View v) {

    }
}
